#include "../import/head.h"
#include "helper_heart.h"
#include "../import/import.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "../helper_net/head.h"
#include "../helper_tcp/head.h"
#include "../helper_udp/head.h"

////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_sgsj
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    helper_heart::helper_heart()
    {
    }

    helper_heart::~helper_heart()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // proc

    int helper_heart::proc()
    {
        int ret = 0;

        //timeout_check();
        //emit_heart();
        show_info();

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int helper_heart::timeout_check()
    {
        int ret = 0;

        session::LIST & s_list = g_pSysData->m_pPool->m_session_list;

        for( session * p = s_list.begin(); ( 0 == ret ) && ( p != s_list.end() ); p = p->m_pNext )
        {
			timeout_check_session( p );
        }

        return ret;
    }

	int helper_heart::timeout_check_session( session * pSession )
	{
		int ret = 0;

		if( ( 0 == ret ) && ( session::TYPE_TCP_LISTEN == pSession->m_eType ) )
		{
			ret = 1;
		}

		if( ( 0 == ret ) && ( session::TYPE_HTTP_LISTEN == pSession->m_eType ) )
		{
			ret = 1;
		}

		if( ( 0 == ret ) && ( pSession->m_bTimeout || pSession->m_bQuiting ) )
		{
			ret = 1;
		}

		if( 0 == ret )
		{
			pSession->net_timeout_s++;
			if( pSession->net_timeout_s < g_pSysData->m_config.net_timeout_s )
			{
				ret = 1;
			}
			else
			{
				pSession->m_bTimeout = true;
				--pSession->m_nRefCount;
			}
		}

		if( ( 0 == ret ) && ( 0 == pSession->m_nRefCount ) )
		{
			if( pSession->m_pTcp )
			{
				net_tcp obj;
				obj.tcp_post_close( pSession->m_pTcp );
			}
			else
			{
				net_udp obj;
				obj.udp_post_close( pSession->m_pUdp );
			}
		}

		return ret;
	}

    int helper_heart::emit_heart()
    {
        int ret = 0;

        helper_protocol obj;
        obj.heart();

        return ret;
    }

    int helper_heart::show_info()
    {
        int ret = 0;

        if( 0 == ret )
        {
            g_pSysData->m_nCurIntervalS++;
            if( g_pSysData->m_nCurIntervalS >= g_pSysData->m_config.show_info_interval_s )
            {
                g_pSysData->m_nCurIntervalS = 0;
            }
            else
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            LOG4( "----------------------------------begin show info-----------------------------------------------" );
            LOG4( "msg num = %d", g_pSysData->m_os.m_pXos->get_total_pool_size( xos::i_xos::XOS_OBJ_MSG, 0 ) );
            LOG4( "buf num = %d", g_pSysData->m_os.m_pXos->get_total_pool_size( xos::i_xos::XOS_OBJ_BUF, 0 ) );
            LOG4( "data num = %d", g_pSysData->m_pPool->m_data_pool.GetTotalCountNoLock() );
            LOG4( "udp num = %d", g_pSysData->m_pPool->m_udp_pool.GetTotalCountNoLock() );
            LOG4( "tcp num = %d", g_pSysData->m_pPool->m_tcp_pool.GetTotalCountNoLock() );
            LOG4( "----------------------------------end show info-----------------------------------------------" );
        }

        return ret;
    }

} // xos_sgsj
